package com.hospital.controller;

import com.hospital.dao.DoctorDao;
import com.hospital.dao.impl.DoctorDaoImpl;
import com.hospital.model.Doctor;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * 添加医生信息处理Servlet
 * 处理管理员添加新医生的请求，验证输入数据并更新数据库
 */
@WebServlet("/admin/addDoctor")
public class AddDoctorServlet extends HttpServlet {

    /** 医生数据访问对象，用于处理医生信息的数据库操作 */
    private DoctorDao doctorDao=new DoctorDaoImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        
        resp.setContentType("application/json;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");

        try {
            // 获取表单数据
            String doctorId = req.getParameter("doctorId");
            
            // 打印接收到的参数
            System.out.println("Received doctorId: " + doctorId);
            
            // 检查医生ID是否已存在
            if (doctorDao.isDoctorIdExists(doctorId)) {
                resp.setStatus(400);
                resp.getWriter().write("{\"error\": \"医生ID已存在\"}");
                return;
            }

            String doctorName = req.getParameter("doctorName");
            String doctorGender = req.getParameter("doctorGender");
            int doctorAge = Integer.parseInt(req.getParameter("doctorAge"));
            String doctorIdCard = req.getParameter("doctorIdCard");
            String doctorPhone = req.getParameter("doctorPhone");
            int deptId = Integer.parseInt(req.getParameter("deptId"));

            // 打印所有参数
            System.out.println("All parameters: " + 
                "name=" + doctorName + 
                ", gender=" + doctorGender + 
                ", age=" + doctorAge + 
                ", idCard=" + doctorIdCard + 
                ", phone=" + doctorPhone + 
                ", deptId=" + deptId);

            // 创建Doctor对象
            Doctor doctor = new Doctor();
            doctor.setDoctorId(doctorId);
            doctor.setDoctorName(doctorName);
            doctor.setDoctorGender(doctorGender.charAt(0));
            doctor.setDoctorAge(doctorAge);
            doctor.setDoctorIdCard(doctorIdCard);
            doctor.setDoctorPhone(doctorPhone);
            doctor.setDeptId(deptId);

            // 保存到数据库
            boolean success = doctorDao.addDoctor(doctor);
            
            if (success) {
                resp.getWriter().write("{\"message\": \"添加成功\"}");
            } else {
                resp.setStatus(500);
                resp.getWriter().write("{\"error\": \"添加失败\"}");
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
            resp.setStatus(500);
            if (e.getMessage().contains("Duplicate entry")) {
                resp.getWriter().write("{\"error\": \"数据库错误：身份证号或工号重复\"}");
            } else {
                resp.getWriter().write("{\"error\": \"数据库错误：" + e.getMessage() + "\"}");
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            resp.setStatus(400);
            resp.getWriter().write("{\"error\": \"数据格式错误：年龄或科室ID必须为数字\"}");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            resp.setStatus(400);
            resp.getWriter().write("{\"error\": \"参数错误：" + e.getMessage() + "\"}");
        } catch (SecurityException e) {
            e.printStackTrace();
            resp.setStatus(403);
            resp.getWriter().write("{\"error\": \"权限错误：" + e.getMessage() + "\"}");
        } catch (IOException e) {
            e.printStackTrace();
            resp.setStatus(500);
            resp.getWriter().write("{\"error\": \"IO错误：" + e.getMessage() + "\"}");
        }
    }
}